<?php

namespace Api\Controller;

class UserController extends ApiController {

    private $error_reg;
    private $error_signin;

    function __construct() {
        parent::__construct();
        $this->error_reg = L('error_reg');
        $this->error_signin = L('error_signin');
    }

    public function register() {
        $username = I('request.username');
        $password = I('request.password');
        $deviceid = I('request.deviceid');
        $group = I('request.group');
        $nickname = I('request.nickname');
        //基本信息验证
        if (!preg_match('/^[a-zA-Z][a-zA-Z0-9_]{7,19}$/', $username)) {
            $this->error($this->error_reg['namematch']);
        }
        if (!preg_match('/^[\x{4e00}-\x{9fa5}]{2,6}$/u', $nickname)) {
            $this->error($this->error_reg['nonick']);
        }
        if (strlen($password) < 8) {
            $this->error($this->error_reg['longerpwd']);
        }
        
        $group = $group != FALSE ? $group : 0;
        $User = D('Users');
        //数据对比
        $where = array('username' => $username);
        if ($User->where($where)->limit(1)->select()) {
            $this->error($this->error_reg['namein']);
        }
        $salt = getsalt();
        $pwd = encryptpwd($password, $salt);
        $token = gettoken($username, $salt);
        $user_id = $User->addUser($username, $pwd, $nickname, $salt, $deviceid, $group);
        if ($user_id) {
            $this->add_signinlog($user_id, $token, $deviceid);
            $arrInfo = array('token' => $token, 'group' => $group);
            $this->success(__FUNCTION__, $arrInfo);
        }
    }

    public function signin() {
        $username = I('request.username');
        $password = I('request.password');
        $deviceid = I('request.deviceid');
        //基本信息验证
        if (!preg_match('/^[a-zA-Z][a-zA-Z0-9_]{7,19}$/', $username)) {
            $this->error($this->error_signin['allmatch']);
        }
        if (strlen($password) < 8) {
            $this->error($this->error_signin['allmatch']);
        }
        //数据对比
        $User = D('Users');
        $where = array('username' => $username);
        $userinfo = $User->where($where)->limit(1)->find();
        if (!$userinfo) {
            $this->error($this->error_signin['allmatch']);
        }
        if ($userinfo['password'] != encryptpwd($password, $userinfo['salt'])) {
            $this->error($this->error_signin['allmatch']);
        }
        $token = gettoken($username, $userinfo['salt']);
        $this->add_signinlog($userinfo['id'], $token, $deviceid);
        $arrInfo = array('token' => $token, 'group' => $userinfo['group']);
        $this->success(__FUNCTION__, $arrInfo);
    }

    /**
     * 更新token
     * @param type $user_id
     * @param type $token
     * @param type $deviceid
     * @return tokenid
     */
    private function add_token($user_id, $token, $deviceid) {
        $data = array(
            'user_id' => $user_id,
            'token' => $token,
            'deviceid' => $deviceid
        );
        return M('Signinlog')->add($data);
    }

    /**
     * 添加登陆log
     * @param type $user_id
     * @param type $token
     * @param type $deviceid
     * @return type
     */
    private function add_signinlog($user_id, $token, $deviceid) {
        $time = date('Y-m-d H:i:s');
        $data = array(
            'user_id' => $user_id,
            'token' => $token,
            'deviceid' => $deviceid,
            'signtime' => $time,
            'createtime' => $time
        );
        return M('Signinlog')->add($data);
    }

}
